<?php

/**
 * This is the Widget for Creating new Term
 * 
 * @author Tuan Nguyen <nganhtuan63@gmail.com>
 * @version 1.0
 * @package  cmswidgets.object
 *
 *
 */
class TermCreateWidget extends CWidget {

    public $visible = true;

    public function init() {
        
    }

    public function run() {
        if ($this->visible) {
            $this->renderContent();
        }
    }

    protected function renderContent() {
        $model = new Term;
        $types_array = ConstantDefine::fileTypes();

        $content_resources = TermResource::Resources();
        //Work with Resource Binding
        $resource = array();
        $resource_upload = array();
        foreach ($content_resources as $res) {
            $resource_upload[] = GxcHelpers::getArrayResourceObjectBinding('resource_upload_' . $res['type']);
        }
        $test = '';
        $i = 0;
        $count_resource = 0;
        foreach ($content_resources as $cres) {
            $j = 1;
            foreach ($resource_upload[$i] as $res_up) {
                $j++;
                $count_resource++;
            }
            $i++;
        }

        $model->total_number_resource = $count_resource;
        //Update Resource Binding Here										
        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'term-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        $id = 0;
        // collect user input data

        if (isset($_POST['Term'])) {

            $model->attributes = $_POST['Term'];
            if ($model->save()) {

                user()->setFlash('success', t('cms', 'Create new Term Successfully!'));
                $i = 0;
                $count_resource = 0;
                
              
                foreach ($content_resources as $cres) {
                    $j = 1;
                    foreach ($resource_upload[$i] as $res_up) {
                        $obj_res = new TermResource();
                        $obj_res->resource_id = $res_up['resid'];
                        $obj_res->term_id = $model->term_id;
                        $obj_res->description = '';
                        $obj_res->type = $cres['type'];
                        $obj_res->resource_order = $j;

                        $obj_res->save();
                        $j++;
                        $count_resource++;
                    }
                    $i++;
                }


                if (!isset($_GET['embed'])) {
                    $model = new Term;
                    Yii::app()->controller->redirect(array('create'));
                }
            }
        }



        Yii::app()->controller->layout = isset($_GET['embed']) ? 'clean' : 'main';
        $this->render('cmswidgets.views.term.term_form_widget', array(
            'model' => $model,
            'types_array' => $types_array,
            'term_id' => '',
            'content_resources' => $content_resources
        ));
    }

}
